import json
from time import sleep

from sympy import false

from  sim_send.send_step import step_1,step_2,step_3
import  login_info.login_info as login
from tools.login_op import get_login_cookie
import re
from tools.log_op import get_cookies_from_txt,get_table_desc,put_json,get_mission_from_txt,wirte_empty_table,get_mission_from_empty,get_stand_desc,put_desc
class river_table_info():
    def test(self):
        print(login.emr_user)
    def __init__(self):
        self.emr_user = login.emr_user
        self.emr_password = login.emr_password
        self.get_cookie_url = login.studio_get_cookie_url
        self.send_job_url = login.send_job_url
        self.chk_job_stats_url_head = login.chk_job_stats_url_head
        self.get_job_result_url_head = login.get_result_url
        self.cookies = self.get_cookies()

    def get_cookies(self):
        cookie = get_cookies_from_txt()
        if(cookie ==''):
            cookie = get_login_cookie(self.get_cookie_url,self.emr_user,self.emr_password,'studio').head_cookie
        return  cookie


    def run_emr_sql(self,sql):
        #step1 start
        print('----------------------------------')
        print('向服务器发送sql语句(step1):')
        print(sql)
        instance = step_1(self,sql)
        print('----------------------------------')
        #step_2 start
        print('\n----------------------------------')
        status = step_2(self,instance)
        while status != 'FINISHED':
            if status == 'ERROR':
                print('stp_2执行失败的sql是 ：',sql)
                return
            print('等待3秒')
            sleep(3)
            status = step_2(self,instance)
        print('step2结束')
        print('----------------------------------')
        #step3 start
        print('\n----------------------------------')
        print('接受服务器返回信息(step3):')
        result = step_3(self,instance)
        print('----------------------------------')
        return result

    def get_rows(self,table_name=''):
        if(table_name==''):
            print('输入表名:')
            table_name=input('')
        self.table_name = table_name
        desc_sql = 'desc '+table_name
        result = self.run_emr_sql(desc_sql)
        detail = result['retData']['detail']
        if_success = detail[1:6]

        if(if_success!=' 运行成功'):
            raise Exception('获取',self.table_name,'列信息失败')
        print('获取',self.table_name,'列信息成功')

        rows = result['retData']['stmtResults'][0]['sqlResult']['rows']
        return rows
    def drop_table(self):
        print('输入表名:')
        sql = input('')
        self.table_name = sql
        desc_sql = 'drop table ' + sql
        print(desc_sql)
        result = self.run_emr_sql(desc_sql)
        #可以复写
        detail = result['retData']['detail']
        if_success = detail[1:6]
        print(if_success)
        if (if_success != ' 运行成功'):
            raise Exception('运行失败')
        rows = result['retData']['stmtResults'][0]['sqlResult']['rows']
        return rows
    def get_columns(self,list_s):
        # 外部的壳
        table_dict = json.loads(get_table_desc())
        #内部元素
        dict_list = []
        for x in list_s:
            print('数据类型:',x[1],'--','列名',x[0],'--','备注:',x[2])
            # dict[x[0]] = {'data_type':x[1],"comment":x[2],"col_name":x[0]}

            if (re.match("^decimal*", x[1])):
                result = re.findall("[1-9]\d*", x[1])
                if(len(result)==1):
                    s = {'columnName': x[0], "columnType": "DECIMAL", "columnNumLen": result[0],"columnNumPercision": 0}
                else:
                    s= {'columnName': x[0], "columnType": "DECIMAL", "columnNumLen":result[0],"columnNumPercision":result[1]}
                dict_list.append(s)
            else:
                s = {'columnName': x[0], "columnType": x[1].upper(), "columnNumLen": "","columnNumPercision":""}
                dict_list.append(s)
            table_dict[self.table_name] = {"columns":dict_list}
        put_json(table_dict)
    def get_stand(self,list_s):
        table_dict = json.loads(get_stand_desc())
        dict_list = []
        for x in list_s:
            print('数据类型:', x[1], '--', '列名', x[0], '--', '备注:', x[2])
            a = {"column_name":x[0],"column_type":x[1],"isCleanRule": "false","keep_mode": "NULL","expressionWord": "","nameEntity": [],}
            dict_list.append(a)
            table_dict[self.table_name] = {"columns":dict_list}
            put_desc(table_dict)
    def make_sql(self,name_list):

        sql_middle= '\t'+'(select count(1) as total from '+ self.table_name +')'+ ','+'\n'
        for index,col_name in enumerate(name_list):
            col = col_name[0]
            if(index != len(name_list)-1):
                sql_middle += str('\t'+ \
                                  '(select count(1) as {}_num from ' + self.table_name + ' where {} is not NULL)' + 'a' + str(index) + ','+'\n').format(col, col)
            else:
                sql_middle += str('\t'+ \
                                  '(select count(1) as {}_num from ' + self.table_name + ' where {} is not NULL)' + 'a' + str(index)).format(col, col)
        sql = 'select * from \n{}'.format(sql_middle)
        return sql




    def win(self,sql):

        rows_result = self.run_emr_sql(sql)
        detail = rows_result['retData']['detail']
        if_success = detail[1:6]
        # print(if_success)
        if (if_success != ' 运行成功'):
            raise Exception('运行失败')
        total = rows_result['retData']['stmtResults'][0]['sqlResult']['rows'][0][0]
        # print('这里是total的总数：',total)
        if(total == '0'):
            wirte_empty_table(self.table_name)


    # def as_select(self,rows):
    #     # sql = "INSERT OVERWRITE table {} \n \t select * FROM (".format(self.std)
    #     sql = "INSERT OVERWRITE table {} \n \t select ".format(self.std)
    #     sql_middle='\n\t\t\tselect'
    #     for index,col_name in enumerate(rows):
    #         col = col_name['col_name']
    #         if(index != len(rows)-1):
    #             sql +=str(col+',')
    #         else:
    #             sql += str(col+' ')
    #     sql += '\n\t\tfrom ( '
    #     list_1 = list(trim_num_full(del_total(self.full)).keys())
    #     list_2 = list(trim_num_small(self.small).keys())
    #     for index,key in enumerate(list_1):
    #         sql_middle += '\n\t\t\t\t' + key + ','
    #
    #     for index, key in enumerate(list_2):
    #         if (index != len(list_2) - 1):
    #             sql_middle += '\n\t\t\t\t' + 'CASE WHEN ' +key+ ' is null then ' + '\'待填写\' ' + 'else '+ key +' end as ' +key +','
    #         if (index == len(list_2) - 1):
    #             sql_middle += '\n\t\t\t\t' + 'CASE WHEN ' +key+ ' is null then ' + '\'待填写\' ' + 'else '+ key +' end as ' +key+' from '+self.table_name+'\n'
    #
    #     # print(sql_middle)
    #     sql_end = ')t'
    #     end = sql + sql_middle + sql_end
    #     print(end)

    def batch_search(self):
        mission = get_mission_from_txt()
        for x in mission:
            rows = self.get_rows(x)
            sql = a.make_sql(rows)
            a.win(sql)
    def batch_table_desc(self):
        mission = get_mission_from_empty()
        mission = [i for i in mission if (len(str(i)) != 0)]
        for x in mission:
            rows = self.get_rows(x)
            self.get_columns(rows)
            self.get_stand(rows)
    def empty_table_desc(self):
        self.batch_search()
        self.batch_table_desc()
if __name__ == '__main__':
    a = river_table_info()
    # # a.drop_table()
    a.batch_table_desc()
    # # # # # #查询出total的个数
    # sql = a.make_sql(rows)
    # a.win(sql)
    # # get_table_desc()
    # mission = get_mission_from_txt()

    # a.batch_search()
    # a.batch_table_desc()
    # rows = a.get_rows('ods_cz_jczl_messageenterconfig')
    # a.get_columns(rows)
